package com.graduationproject.examonline.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Token {

    public static String createToken(String userId, String identity, String userName, Date overDate, String key) {
        try {
            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put("userId", userId);
            claims.put("identity", identity);
            claims.put("userName", userName);
            String token = Jwts.builder()
                    .setClaims(claims)
                    .signWith(SignatureAlgorithm.HS256, key)
//                    .setExpiration(overDate)
                    .compact();
            return token;
        } catch (Exception e) {
            System.out.println(e);
            return "error";
        }
    }

    public static Claims parseToken(String token, String key) {
        Claims body = null;
        try {
            body = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
            System.out.println(body.toString());
            return body;
        } catch (JwtException e) {
            e.printStackTrace();
            return body;
        }
    }
}
